package com.atf.atfconfigurator.composite;

import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.event.LayoutEvents.LayoutClickListener;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;

public class CollapseableBox extends CustomComponent{
	private static final long serialVersionUID = 632703177884323377L;
    final private CssLayout root;
    private CssLayout bodyContainer;
    
    void toggleBodyVisible(){
        bodyContainer.setVisible(!bodyContainer.isVisible());
    }
    
    public CollapseableBox(String title, Component body) {
        super();
        root = new CssLayout();
        root.addStyleName("collapseblebox-container");
        setCompositionRoot(root);
        draw(title, body,false);
    }
    public CollapseableBox(String title, Component body,boolean isvisible) {
        super();
        root = new CssLayout();
        root.addStyleName("collapseblebox-container");
        setCompositionRoot(root);
        draw(title, body,isvisible);
    }

    void draw(String title, Component body, boolean isvisible) {
       // CssLayout titleLayout = new CssLayout();
      //  titleLayout.addStyleName("collapsablebox-title");
        
        final Button button = new Button(title);
        if( isvisible){
        	button.setIcon(FontAwesome.MINUS_SQUARE);
        }else{
        	 button.setIcon(FontAwesome.PLUS_SQUARE);
        }
       
        button.addStyleName("borderless-colored");
        
   
       // titleLayout.addComponent(button);
        button.addClickListener(new ClickListener() {
            private static final long serialVersionUID = -4750845792730551399L;

			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				if( button.getIcon().equals(FontAwesome.PLUS_SQUARE)){
					button.setIcon(FontAwesome.MINUS_SQUARE);
				}else{
					 button.setIcon(FontAwesome.PLUS_SQUARE);
				}
				toggleBodyVisible();
				
			}
            
        });
        
        
        bodyContainer = new CssLayout();
        bodyContainer.addStyleName("collapsablebox-body");
        bodyContainer.addComponent(body);
        bodyContainer.setVisible(isvisible);
        root.addComponent(button);
        root.addComponent(bodyContainer);
    }
}
